package com.glodon.paas.search.util;

import javax.ws.rs.core.MediaType;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.glodon.paas.document.api.bean.File;
import com.glodon.paas.exception.PaasServerExceptionFactory;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

/**
 * 负责调用document的服务.
 * 
 * @author botao.liao
 * 
 */
@Component
public class DocWebService extends AbstractWebService {
    @Value("${api.internal.document.url}")
    private String documentInternalUrl;

    /**
     * 从document中提取File meta信息
     * 
     * @param fileId
     * @param userId
     * @return
     */
    public File getFileMeta(String fileId, String userId) {
        if (StringUtils.isEmpty(fileId) || StringUtils.isEmpty(userId)) {
            throw PaasServerExceptionFactory.validateException("fileId is empty in databag.", null);
        }
        String url = documentInternalUrl.trim() + "/file/" + fileId + "?meta&type=id&userId=" + userId;
        WebResource resource = Client.create(clientConfig).resource(url);
        ClientResponse c = resource.type(MediaType.APPLICATION_JSON_TYPE).get(ClientResponse.class);
        if (c.getStatus() != 200) {
            throw PaasServerExceptionFactory.internalAPIException(
                    "get file metadata failed. " + c.getEntity(String.class), null);
        }
        // @SuppressWarnings("unchecked")
        // Map<String, ?> map = c.getEntity(Map.class);
        // File file = new File();
        // file.setId((String) map.get("id"));
        // file.setParentId((String) map.get("parentId"));
        // file.setProjectId((String) map.get("projectId"));
        // file.setName((String) map.get("name"));
        // file.setExtension((String) map.get("extension"));
        // file.setOwnerId((String) map.get("ownerId"));
        // file.setUpdator((String) map.get("updator"));
        // file.setCreator((String) map.get("creator"));
        // file.setCreateTime(new Date((Long) map.get("createTime")));
        // file.setUpdateTime(new Date((Long) map.get("updateTime")));
        // file.setCount((Integer) map.get("count"));
        // file.setSize((Long) map.get("size"));
        // file.setFolder(Boolean.valueOf((String) map.get("folder")));
        // file.setShared(Boolean.valueOf((String) map.get("shared")));
        // file.setPublished(Boolean.valueOf((String) map.get("published")));
        // file.setFolderCount(Integer.parseInt((String)
        // map.get("folderCount")));
        // file.setDataBagId((String) map.get("databagId"));
        // file.setRevisionNumber((Integer) map.get("revisionNumber"));
        File file = c.getEntity(File.class);
        return file;
    }

    public void setDocumentInternalUrl(String documentInternalUrl) {
        this.documentInternalUrl = documentInternalUrl;
    }
}
